package araujoCoronelVillamayor.plantasVsZombies.uiWicket;

import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.SesionAppModel;
import araujoCoronelVillamayor.plantasVsZombies.domain.Fila;
import araujoCoronelVillamayor.plantasVsZombies.domain.Jardin;
import araujoCoronelVillamayor.plantasVsZombies.domain.Planta;
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.EstadoJardinPanel;
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.JardinPage;
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.layouts.LayoutPrincipal;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure0;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.uqbar.commons.model.UserException;
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods;
import org.uqbar.wicket.xtend.XButton;

@SuppressWarnings("all")
public class PlantacionPage extends LayoutPrincipal {
  @Extension
  private WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new Function0<WicketExtensionFactoryMethods>() {
    public WicketExtensionFactoryMethods apply() {
      WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new WicketExtensionFactoryMethods();
      return _wicketExtensionFactoryMethods;
    }
  }.apply();
  
  private final JardinPage mainPage;
  
  private String filaId;
  
  private String tipoFila;
  
  private String mensajeOk;
  
  public PlantacionPage(final Fila filaParametro, final SesionAppModel sesionAppModelParametro, final JardinPage mainPage) {
    super();
    this.mainPage = mainPage;
    CompoundPropertyModel<SesionAppModel> _compoundPropertyModel = new CompoundPropertyModel<SesionAppModel>(sesionAppModelParametro);
    this.setDefaultModel(_compoundPropertyModel);
    SesionAppModel _sesion = this.getSesion();
    _sesion.setFilaSeleccionada(filaParametro);
    SesionAppModel _sesion_1 = this.getSesion();
    CompoundPropertyModel<SesionAppModel> _asCompoundModel = this._wicketExtensionFactoryMethods.<SesionAppModel>asCompoundModel(_sesion_1);
    Form<SesionAppModel> _form = new Form<SesionAppModel>("nuevaPlantaForm", _asCompoundModel);
    final Form<SesionAppModel> plantarForm = _form;
    DropDownChoice<Planta> _dropDownChoice = new DropDownChoice<Planta>("plantaSeleccionada");
    final Procedure1<DropDownChoice<Planta>> _function = new Procedure1<DropDownChoice<Planta>>() {
      public void apply(final DropDownChoice<Planta> it) {
        final Function0<List<Planta>> _function = new Function0<List<Planta>>() {
          public List<Planta> apply() {
            SesionAppModel _sesion = PlantacionPage.this.getSesion();
            Fila _filaSeleccionada = _sesion.getFilaSeleccionada();
            List<Planta> _plantasDelMismoTipo = _filaSeleccionada.plantasDelMismoTipo();
            return _plantasDelMismoTipo;
          }
        };
        LoadableDetachableModel<List<Planta>> _loadableModel = PlantacionPage.this._wicketExtensionFactoryMethods.<List<Planta>>loadableModel(_function);
        it.setChoices(_loadableModel);
        final Function1<Planta,String> _function_1 = new Function1<Planta,String>() {
          public String apply(final Planta p) {
            String _nombre = p.getNombre();
            return _nombre;
          }
        };
        ChoiceRenderer<Planta> _choiceRenderer = PlantacionPage.this._wicketExtensionFactoryMethods.<Planta>choiceRenderer(_function_1);
        it.setChoiceRenderer(_choiceRenderer);
      }
    };
    DropDownChoice<Planta> _doubleArrow = ObjectExtensions.<DropDownChoice<Planta>>operator_doubleArrow(_dropDownChoice, _function);
    this._wicketExtensionFactoryMethods.addChild(plantarForm, _doubleArrow);
    XButton _xButton = new XButton("botonPlantar");
    final Procedure1<XButton> _function_1 = new Procedure1<XButton>() {
      public void apply(final XButton it) {
        final Procedure0 _function = new Procedure0() {
          public void apply() {
            PlantacionPage.this.plantar();
          }
        };
        it.setOnClick(_function);
      }
    };
    XButton _doubleArrow_1 = ObjectExtensions.<XButton>operator_doubleArrow(_xButton, _function_1);
    this._wicketExtensionFactoryMethods.addChild(plantarForm, _doubleArrow_1);
    XButton _xButton_1 = new XButton("volverJardin");
    final Procedure1<XButton> _function_2 = new Procedure1<XButton>() {
      public void apply(final XButton it) {
        final Procedure0 _function = new Procedure0() {
          public void apply() {
            PlantacionPage.this.volver();
          }
        };
        it.setOnClick(_function);
      }
    };
    XButton _doubleArrow_2 = ObjectExtensions.<XButton>operator_doubleArrow(_xButton_1, _function_2);
    this._wicketExtensionFactoryMethods.addChild(plantarForm, _doubleArrow_2);
    SesionAppModel _sesion_2 = this.getSesion();
    Fila _filaSeleccionada = _sesion_2.getFilaSeleccionada();
    String _tipo = _filaSeleccionada.getTipo();
    String _lowerCase = _tipo.toLowerCase();
    this.tipoFila = _lowerCase;
    Label _label = new Label("tipoFila", this.tipoFila);
    this._wicketExtensionFactoryMethods.addChild(plantarForm, _label);
    this._wicketExtensionFactoryMethods.addChild(this, plantarForm);
    SesionAppModel _sesion_3 = this.getSesion();
    Jardin _jardin = _sesion_3.getJardin();
    ArrayList<Fila> _filas = _jardin.getFilas();
    SesionAppModel _sesion_4 = this.getSesion();
    Fila _filaSeleccionada_1 = _sesion_4.getFilaSeleccionada();
    int _indexOf = _filas.indexOf(_filaSeleccionada_1);
    int _plus = (_indexOf + 1);
    String _string = Integer.valueOf(_plus).toString();
    String _plus_1 = (_string + " (");
    String _plus_2 = (_plus_1 + this.tipoFila);
    String _plus_3 = (_plus_2 + ")");
    this.filaId = _plus_3;
    Label _label_1 = new Label("filaId", this.filaId);
    this._wicketExtensionFactoryMethods.addChild(this, _label_1);
    SesionAppModel _sesion_5 = this.getSesion();
    Fila _filaSeleccionada_2 = _sesion_5.getFilaSeleccionada();
    PropertyModel<Object> _propertyModel = new PropertyModel<Object>(_filaSeleccionada_2, "lugaresLibres");
    Label _label_2 = new Label("lugaresLibres", _propertyModel);
    this._wicketExtensionFactoryMethods.addChild(this, _label_2);
    PropertyModel<Object> _propertyModel_1 = new PropertyModel<Object>(this, "mensajeOk");
    Label _label_3 = new Label("mensajeOk", _propertyModel_1);
    this._wicketExtensionFactoryMethods.addChild(this, _label_3);
    FeedbackPanel _feedbackPanel = new FeedbackPanel("feedbackPanel");
    this._wicketExtensionFactoryMethods.addChild(this, _feedbackPanel);
    SesionAppModel _sesion_6 = this.getSesion();
    EstadoJardinPanel _estadoJardinPanel = new EstadoJardinPanel("panelEstadoJardin", _sesion_6);
    final EstadoJardinPanel panelEstadoJardin = _estadoJardinPanel;
    this._wicketExtensionFactoryMethods.addChild(this, panelEstadoJardin);
  }
  
  public String plantar() {
    String _xblockexpression = null;
    {
      this.mensajeOk = "";
      String _xtrycatchfinallyexpression = null;
      try {
        String _xblockexpression_1 = null;
        {
          SesionAppModel _sesion = this.getSesion();
          _sesion.poner();
          SesionAppModel _sesion_1 = this.getSesion();
          Planta _plantaSeleccionada = _sesion_1.getPlantaSeleccionada();
          String _nombre = _plantaSeleccionada.getNombre();
          String _plus = ("Se plantó " + _nombre);
          String _plus_1 = (_plus + ".");
          String _mensajeOk = this.mensajeOk = _plus_1;
          _xblockexpression_1 = (_mensajeOk);
        }
        _xtrycatchfinallyexpression = _xblockexpression_1;
      } catch (final Throwable _t) {
        if (_t instanceof UserException) {
          final UserException e = (UserException)_t;
          String _message = e.getMessage();
          this.error(_message);
        } else {
          throw Exceptions.sneakyThrow(_t);
        }
      }
      _xblockexpression = (_xtrycatchfinallyexpression);
    }
    return _xblockexpression;
  }
  
  public void volver() {
    this.mainPage.refreshFilas();
    this.setResponsePage(this.mainPage);
  }
  
  public SesionAppModel getSesion() {
    Object _defaultModelObject = this.getDefaultModelObject();
    return ((SesionAppModel) _defaultModelObject);
  }
}
